package com.tsunami.auth.service.auth.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsunami.auth.mapper.UserMapper;
import com.tsunami.auth.service.auth.IUserRoleService;
import com.tsunami.auth.service.auth.IUserService;
import com.tsunami.authority.dto.auth.UserDTO;
import com.tsunami.authority.dto.auth.UserSaveDTO;
import com.tsunami.authority.dto.auth.UserUpdateDTO;
import com.tsunami.authority.dto.auth.UserUpdatePasswordDTO;
import com.tsunami.authority.entity.auth.User;
import com.tsunami.base.R;
import com.tsunami.exception.BizException;
import com.tsunami.exception.code.ExceptionCode;
import com.tsunami.user.model.SysUser;
import com.tsunami.utils.PageUtils;
import com.tsunami.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lx
 * @since 2021-09-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IUserRoleService userRoleService;

    @Override
    public User selectByAccount(String username) {
        return baseMapper.selectByAccount(username);
    }

    @Override
    public R selectPageList(UserDTO user) {
        Page<User> pages = baseMapper.selectPageList(PageUtils.buildPage(),user);
        return R.ok().data(pages);
    }

    @Override
    public User saveUser(SysUser loginUser,UserSaveDTO user) {
        User u = new User();
//        u.setTenantId();
        u.setAccount(user.getAccount());
        u.setName(user.getName());
        u.setOrgId(user.getOrgId());
        u.setStationId(user.getStationId());
        u.setEmail(user.getEmail());
        u.setMobile(user.getMobile());
        u.setSex(user.getSex());
        u.setStatus(user.getStatus());
        u.setAvatar(user.getAvatar());
        u.setWorkDescribe(user.getWorkDescribe());
        u.setPasswordErrorLastTime(LocalDateTime.now());
        u.setPasswordErrorNum(0);
        //永不过期
        u.setPasswordExpireTime(null);
//        u.setPassword(MD5.encrypt(user.getPassword()));
        u.setLastLoginTime(LocalDateTime.now());
        u.setCreateUser(loginUser.getId());
        super.save(u);
        return u;
    }

    @Override
    public User updateUser(SysUser loginUser,UserUpdateDTO data) {
        User user = super.getById(data.getId());
        user.setPasswordExpireTime(null);
        if(StringUtils.isNotEmpty(data.getAvatar())){
            user.setAvatar(data.getAvatar());
        }
        if(StringUtils.isNotEmpty(data.getEmail())){
            user.setEmail(data.getEmail());
        }
        if(StringUtils.isNotEmpty(data.getMobile())){
            user.setMobile(data.getMobile());
        }
        if(StringUtils.isNotEmpty(data.getName())){
            user.setName(data.getName());
        }
        if(data.getOrgId()!=null){
            user.setOrgId(data.getOrgId());
        }
        if(data.getSex()!=null){
            user.setSex(data.getSex());
        }
        if(data.getStationId()!=null){
            user.setStationId(data.getStationId());
        }
        if(StringUtils.isNotEmpty(data.getStatus())){
            user.setStatus(data.getStatus());
        }
        if(StringUtils.isNotEmpty(data.getWorkDescribe())){
            user.setWorkDescribe(data.getWorkDescribe());
        }
        user.setUpdateUser(loginUser.getId());
        super.updateById(user);
        return user;
    }

    @Override
    public Boolean updatePassword(UserUpdatePasswordDTO data) {
        if(!data.getConfirmPassword().equals(data.getPassword())){
            throw new BizException(ExceptionCode.NOSAMEPASSWORD);
        }
        User user = getById(data.getId());
        if(user==null){
            throw new BizException(ExceptionCode.NO_USER);
        }
        String oldPassword = "MD5.encrypt(data.getOldPassword())";
        if(!user.getPassword().equals(oldPassword)){
            throw new BizException(ExceptionCode.EX_USER_OR_PASSWORD);
        }
        User build = User.builder()
//                .password(MD5.encrypt(data.getPassword()))
                .id(data.getId())
                .updateTime(LocalDateTime.now())
                .build();
        this.updateById(build);
        return true;
    }

    @Override
    public Boolean reset(List<Long> ids) {
        LocalDateTime passwordExpireTime = null;
        String defPassword = "MD5.encrypt(zl12345L)";
        if(ids!=null&&ids.size()>0){
            for(Long id:ids){
               User user = getById(id);
               user.setPassword(defPassword);
               user.setPasswordErrorNum(0);
               user.setPasswordErrorLastTime(null);
               user.setPasswordExpireTime(passwordExpireTime);
               super.updateById(user);
            }
        }
        return true;
    }

    @Override
    public boolean remove(List<Long> ids) {
        QueryWrapper param = new QueryWrapper();
        param.in("user_id",ids);
        userRoleService.remove(param);
        return super.removeByIds(ids);
    }

    @Override
    public List<User> findUserByRoleId(Long roleId, String keyword) {
        return baseMapper.findUserByRoleId(roleId,keyword);
    }

    @Override
    public void updateLoginTime(String account) {
        baseMapper.updateLastLoginTime(account, LocalDateTime.now());
    }
}